import { message } from 'antd';
import { routerRedux } from 'dva/router';
import { getStatus, setStatus, getStatusList, getStatusGroupList } from '../services/status';
import  { List } from 'immutable';

export default {
  namespace: 'status',
  state: {
    status: 2,
    statusList: [],
    statusGroup: 1,
    statusGroupList: []
  },

  effects: {

    *setStatus({ status }, { call, put }) {
      const res = yield setStatus(status);
      if (!res || res.err) {
        message.error('提交失败');
      } else {
        message.success('提交成功');
        yield put({ type: 'save', payload: { status }});
      }
    },

    *getStatus({ }, { call, put }) {
      const res = yield getStatus();
      if (res.err && res.err.response && (res.err.response.status === 401)) {
        yield put(routerRedux.push('/login'));
      } else if (!res.err && res.data.data && (res.data.data.status === 200)){
        const { status } = res.data.data;
        yield put({ type: 'save', payload: { status }});
      }
    },

    *getStatusList({ }, { call, put }) {
      const res = yield getStatusList();
      if (!res || res.err) {
        yield put(routerRedux.push('/login'));
      } else {
        const newList = res.data.data;
        yield put({ type: 'saveList', payload: { newList }});
      }
    },

    *getStatusGroupList({ payload }, { call, put }) {
      const { group } = payload;
      const res = yield getStatusGroupList(group);
      if (!res || res.err) {
        yield put(routerRedux.push('/login'));
      } else {
        const newList = res.data.data;
        yield put({ type: 'saveGroupList', payload: { newList }});
      }
    },
  },

  reducers: {
    'save'(state, action) {
      return { ...state, ...action.payload };
    },
    'saveGroupList'(state, action) {
      const { newList } = action.payload;
      const oldList = state.statusGroupList;
      const list = newList.map(item => {
        const { userid, status, username, hasNewMessage } = item;
        let type = '正常';
        switch (status) {
          case 0:
            type = '非常忙';
            break;
          case 1:
            type = '忙';
            break;
          case 2:
            type = '正常';
            break;
          case 3:
            type = '不饱满';
            break;
        }
        let messageType = '否';
        if (hasNewMessage) {
          messageType = '是';
        }
        return {
          key: userid,
          userid,
          username,
          status: type,
          hasNewMessage: messageType
        }
      })
      if (JSON.stringify(oldList) == JSON.stringify(list)) {
        return {...state}
      } else {
        return {...state, statusGroupList: list}
      }
    },
    'saveList'(state, action) {
      const { newList } = action.payload;
      const oldList = state.statusList;

      const list = newList.filter(item => {
        const { value } = item;
        if (value != 0) {
          return item;
        }
      })
      .map(item => {
        const { group, status, value } = item;
        let type = '正常';
          switch (status) {
            case 0:
              type = '非常忙';
              break;
            case 1:
              type = '忙';
              break;
            case 2:
              type = '正常';
              break;
            case 3:
              type = '不饱满';
              break;
          }

          let newGroup;
          switch (group) {
            case 0:
              newGroup = '技术组';
              break;
            case 1:
              newGroup = '一组';
              break;
            case 2:
              newGroup = '二组';
              break;
            case 3:
              newGroup = '三组';
              break;
            case 4:
              newGroup = '四组';
              break;
            case 5:
              newGroup = '五组';
              break;
            case 6:
              newGroup = '六组';
              break;
          }
          return {
            group: newGroup,
            value,
            type,
          }
      });
      if (JSON.stringify(oldList) == JSON.stringify(list)) {
        return {...state}
      } else {
        return {...state, statusList: list}
      }
    }
  },
};
